//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

public class demo_3439 {
    public demo_3439() {
    }

    public int maxFreeTime(int eventTime, int k, int[] startTime, int[] endTime) {
        int[] res = new int[startTime.length + 1];
        int n = res.length;
        res[0] = startTime[0];
        res[n - 1] = eventTime - endTime[n - 2];

        for(int i = 1; i < n - 1; ++i) {
            res[i] = startTime[i] - endTime[i - 1];
        }

        int max = -1;
        int sum = 0;
        ++k;

        for(int i = 0; i < n; ++i) {
            sum += res[i];
            if (i >= k - 1) {
                max = Math.max(max, sum);
                sum -= res[i - (k - 1)];
            }
        }

        return max;
    }
}
